/*
 * OptionsWindow.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.settings.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.lailaps.gui.GUIBuilder;
import org.tiberiumlabs.lailaps.settings.Settings;
import org.tiberiumlabs.lailaps.toolkit.Constants;

/**
 *
 * @author <a href="mailto:beast.tiberiumlabs@gmail.com">Beast</a>
 */
public class OptionsWindow extends javax.swing.JPanel {

    private javax.swing.JLabel selectedLabel;
    private Window hostWindow;
    private Hashtable<String, OptionPanel> panelCache = new Hashtable<String, OptionPanel>();

    private ResourceBundle bundle = ResourceBundle.getBundle("org/tiberiumlabs/lailaps/resources/messages/settings_window");

    private static final Color mouseOverClr = new Color(224, 232, 246);
    private static final Color selectedClr = new Color(193, 210, 238);
    private static final Border invisibleBorder = new LineBorder(Color.WHITE, 1);
    private static final Border visibleBorder = new LineBorder(new Color(202, 181, 226), 1);
    private static final Border selectedBorder = new LineBorder(new Color(168, 133, 208), 1);

    private static final String cancelCommandName = "CancelCommand:Dispose";
    private boolean firstTime;

    /** Creates new form OptionsWindow */
    public OptionsWindow(Window host, boolean firstTime) {
        initComponents();
        hostWindow = host;
        this.firstTime = firstTime;
        selectedLabel = generalOpsLabel;
        optionBodyPanel.setLayout(new BorderLayout(0, 0));
        GeneralOptionPanel genPanel = new GeneralOptionPanel();
        panelCache.put("General", genPanel);
        optionBodyPanel.add(genPanel, BorderLayout.NORTH);
        ((JFrame)host).getRootPane().setDefaultButton(okButton);

        if (firstTime) {
            cancelButton.setEnabled(false);
            okButton.setAction(new OkActionFirstTime());
        } else {
            okButton.setAction(new OkAction());
        }

        KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        ((JFrame)host).getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, cancelCommandName);
        ((JFrame)host).getRootPane().getActionMap().put(cancelCommandName, cancelButton.getAction());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        optionChooserPanel = new javax.swing.JPanel();
        generalOpsLabel = new javax.swing.JLabel();
        networkOpsLabel = new javax.swing.JLabel();
        appearanceLabel = new javax.swing.JLabel();
        optionPageTitlePanel = new javax.swing.JPanel();
        opTitleLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        optionBodyPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();

        optionChooserPanel.setBackground(new java.awt.Color(255, 255, 255));
        optionChooserPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        generalOpsLabel.setBackground(new java.awt.Color(193, 210, 238));
        generalOpsLabel.setFont(new java.awt.Font("Dialog", 0, 10));
        generalOpsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        generalOpsLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/tiberiumlabs/lailaps/resources/images/ops_general.png")));
        generalOpsLabel.setText(bundle.getString("generalOpsLabel")); // NOI18N
        generalOpsLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(176, 143, 212)));
        generalOpsLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        generalOpsLabel.setIconTextGap(2);
        generalOpsLabel.setMaximumSize(new java.awt.Dimension(32, 39));
        generalOpsLabel.setMinimumSize(new java.awt.Dimension(32, 39));
        generalOpsLabel.setOpaque(true);
        generalOpsLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        generalOpsLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                generalOpsLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                generalOpsLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                generalOpsLabelMouseExited(evt);
            }
        });

        networkOpsLabel.setBackground(new java.awt.Color(255, 255, 255));
        networkOpsLabel.setFont(new java.awt.Font("Dialog", 0, 10));
        networkOpsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        networkOpsLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/tiberiumlabs/lailaps/resources/images/ops_network.png")));
        networkOpsLabel.setText(bundle.getString("networkOpsLabel")); // NOI18N
        networkOpsLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        networkOpsLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        networkOpsLabel.setIconTextGap(2);
        networkOpsLabel.setOpaque(true);
        networkOpsLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        networkOpsLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                networkOpsLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                networkOpsLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                networkOpsLabelMouseExited(evt);
            }
        });

        appearanceLabel.setBackground(new java.awt.Color(255, 255, 255));
        appearanceLabel.setFont(new java.awt.Font("Dialog", 0, 10));
        appearanceLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        appearanceLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/tiberiumlabs/lailaps/resources/images/ops_apperance.png")));
        appearanceLabel.setText(bundle.getString("appearanceLabel")); // NOI18N
        appearanceLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        appearanceLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        appearanceLabel.setIconTextGap(2);
        appearanceLabel.setOpaque(true);
        appearanceLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        appearanceLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                appearanceLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                appearanceLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                appearanceLabelMouseExited(evt);
            }
        });

        javax.swing.GroupLayout optionChooserPanelLayout = new javax.swing.GroupLayout(optionChooserPanel);
        optionChooserPanel.setLayout(optionChooserPanelLayout);
        optionChooserPanelLayout.setHorizontalGroup(
            optionChooserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionChooserPanelLayout.createSequentialGroup()
                .addGroup(optionChooserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(generalOpsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(appearanceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(networkOpsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        optionChooserPanelLayout.setVerticalGroup(
            optionChooserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionChooserPanelLayout.createSequentialGroup()
                .addComponent(generalOpsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(networkOpsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(appearanceLabel)
                .addContainerGap(231, Short.MAX_VALUE))
        );

        optionPageTitlePanel.setBackground(new java.awt.Color(255, 255, 255));
        optionPageTitlePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        opTitleLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/tiberiumlabs/lailaps/resources/images/ops_general.png")));
        opTitleLabel.setText("General");

        javax.swing.GroupLayout optionPageTitlePanelLayout = new javax.swing.GroupLayout(optionPageTitlePanel);
        optionPageTitlePanel.setLayout(optionPageTitlePanelLayout);
        optionPageTitlePanelLayout.setHorizontalGroup(
            optionPageTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionPageTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(opTitleLabel)
                .addContainerGap(365, Short.MAX_VALUE))
        );
        optionPageTitlePanelLayout.setVerticalGroup(
            optionPageTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(opTitleLabel)
        );

        cancelButton.setAction(new CancelAction());
        cancelButton.setFont(new java.awt.Font("Dialog", 0, 12));
        cancelButton.setText(bundle.getString("cancel.button.title")); // NOI18N

        applyButton.setAction(new ApplyAction());
        applyButton.setFont(new java.awt.Font("Dialog", 0, 12));
        applyButton.setText(bundle.getString("apply.button.title")); // NOI18N

        javax.swing.GroupLayout optionBodyPanelLayout = new javax.swing.GroupLayout(optionBodyPanel);
        optionBodyPanel.setLayout(optionBodyPanelLayout);
        optionBodyPanelLayout.setHorizontalGroup(
            optionBodyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 464, Short.MAX_VALUE)
        );
        optionBodyPanelLayout.setVerticalGroup(
            optionBodyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 343, Short.MAX_VALUE)
        );

        okButton.setFont(new java.awt.Font("Dialog", 0, 12));
        okButton.setText(bundle.getString("ok.button.title")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(optionChooserPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(optionBodyPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(optionPageTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(313, 313, 313)
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(applyButton, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(optionPageTitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(optionBodyPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(optionChooserPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton)
                    .addComponent(applyButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void generalOpsLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_generalOpsLabelMouseClicked
        if (selectedLabel != generalOpsLabel) {
            selectedLabel.setBackground(Color.WHITE);
            selectedLabel.setBorder(invisibleBorder);
            generalOpsLabel.setBackground(selectedClr);
            generalOpsLabel.setBorder(selectedBorder);
            selectedLabel = generalOpsLabel;

            opTitleLabel.setIcon(generalOpsLabel.getIcon());
            opTitleLabel.setText(generalOpsLabel.getText());

            optionBodyPanel.removeAll();
            GeneralOptionPanel genPanel = (GeneralOptionPanel)panelCache.get("General");
            if (genPanel != null) {
                optionBodyPanel.add(genPanel, BorderLayout.NORTH);
            } else {
                genPanel = new GeneralOptionPanel();
                panelCache.put("General", genPanel);
                optionBodyPanel.add(genPanel, BorderLayout.NORTH);
            }
            optionBodyPanel.repaint();
        }
    }//GEN-LAST:event_generalOpsLabelMouseClicked

    private void networkOpsLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_networkOpsLabelMouseClicked
        if (selectedLabel != networkOpsLabel) {
            selectedLabel.setBackground(Color.WHITE);
            selectedLabel.setBorder(invisibleBorder);
            networkOpsLabel.setBackground(selectedClr);
            networkOpsLabel.setBorder(selectedBorder);
            selectedLabel = networkOpsLabel;

            opTitleLabel.setIcon(networkOpsLabel.getIcon());
            opTitleLabel.setText(networkOpsLabel.getText());

            optionBodyPanel.removeAll();
            NetworkOptionsPanel netPanel = (NetworkOptionsPanel)panelCache.get("Network");
            if (netPanel != null) {
                optionBodyPanel.add(netPanel, BorderLayout.NORTH);
            } else {
                netPanel = new NetworkOptionsPanel();
                panelCache.put("Network", netPanel);
                optionBodyPanel.add(netPanel, BorderLayout.NORTH);
            }
            optionBodyPanel.repaint();
        }
    }//GEN-LAST:event_networkOpsLabelMouseClicked

    private void appearanceLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_appearanceLabelMouseClicked
        if (selectedLabel != appearanceLabel) {
            selectedLabel.setBackground(Color.WHITE);
            selectedLabel.setBorder(invisibleBorder);
            appearanceLabel.setBackground(selectedClr);
            appearanceLabel.setBorder(selectedBorder);
            selectedLabel = appearanceLabel;

            opTitleLabel.setIcon(appearanceLabel.getIcon());
            opTitleLabel.setText(appearanceLabel.getText());

            optionBodyPanel.removeAll();
            ApperaceOptionsPanel apearPanel = (ApperaceOptionsPanel)panelCache.get("Aperance");
            if (apearPanel != null) {
                optionBodyPanel.add(apearPanel, BorderLayout.NORTH);
            } else {
                apearPanel = new ApperaceOptionsPanel(firstTime);
                panelCache.put("Aperance", apearPanel);
                optionBodyPanel.add(apearPanel, BorderLayout.NORTH);
            }
            optionBodyPanel.repaint();
        }
    }//GEN-LAST:event_appearanceLabelMouseClicked

    private void appearanceLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_appearanceLabelMouseExited
        if (selectedLabel != appearanceLabel) {
            appearanceLabel.setBackground(Color.WHITE);
            appearanceLabel.setBorder(invisibleBorder);
        }
    }//GEN-LAST:event_appearanceLabelMouseExited

    private void appearanceLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_appearanceLabelMouseEntered
        if (selectedLabel != appearanceLabel) {
            appearanceLabel.setBackground(mouseOverClr);
            appearanceLabel.setBorder(visibleBorder);
        }
    }//GEN-LAST:event_appearanceLabelMouseEntered

    private void networkOpsLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_networkOpsLabelMouseExited
        if (selectedLabel != networkOpsLabel) {
            networkOpsLabel.setBackground(Color.WHITE);
            networkOpsLabel.setBorder(invisibleBorder);
        }
    }//GEN-LAST:event_networkOpsLabelMouseExited

    private void networkOpsLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_networkOpsLabelMouseEntered
        if (selectedLabel != networkOpsLabel) {
            networkOpsLabel.setBackground(mouseOverClr);
            networkOpsLabel.setBorder(visibleBorder);
        }
    }//GEN-LAST:event_networkOpsLabelMouseEntered

    private void generalOpsLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_generalOpsLabelMouseEntered
        if (selectedLabel != generalOpsLabel) {
            generalOpsLabel.setBackground(mouseOverClr);
            generalOpsLabel.setBorder(visibleBorder);
        }
    }//GEN-LAST:event_generalOpsLabelMouseEntered

    private void generalOpsLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_generalOpsLabelMouseExited
        if (selectedLabel != generalOpsLabel) {
            generalOpsLabel.setBackground(Color.WHITE);
            generalOpsLabel.setBorder(invisibleBorder);
        }
    }//GEN-LAST:event_generalOpsLabelMouseExited

    private class OkAction extends AbstractAction {

        public OkAction() {
            putValue(NAME, bundle.getString("ok.button.title"));
        }

        public void actionPerformed(ActionEvent e) {
            for (OptionPanel panel : panelCache.values()) {
                panel.okAction();
            }
            hostWindow.dispose();
        }
    }

    private class OkActionFirstTime extends AbstractAction {

        public OkActionFirstTime() {
            putValue(NAME, bundle.getString("ok.button.title"));
        }

        public void actionPerformed(ActionEvent e) {
            for (OptionPanel panel : panelCache.values()) {
                panel.okAction();
            }
            hostWindow.dispose();

            if (!Settings.getApplicationSettings().isUseSystemLocale()) {
                String userLocale = Settings.getApplicationSettings().getCustomLocale();
                if (userLocale.equals(Constants.ENGLISH_LNG)) {
                    Locale.setDefault(new Locale("en", "US"));
                } else if (userLocale.equals(Constants.RUSSIAN_LNG)) {
                    Locale.setDefault(new Locale("ru", "RU"));
                } else if (userLocale.equals(Constants.UKRAINIAN_LNG)) {
                    Locale.setDefault(new Locale("uk", "UA"));
                }
            }
            GUIBuilder guiBuilder = Mediator.getMediator().getGuiBuilder();
            guiBuilder.getMainWindow().setVisible(true);
            guiBuilder.getDropTarget().setVisible(true);
        }
    }

    private class ApplyAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            for (OptionPanel panel : panelCache.values()) {
                panel.applyAction();
            }
        }
    }

    private class CancelAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            hostWindow.dispose();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel appearanceLabel;
    private javax.swing.JButton applyButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel generalOpsLabel;
    private javax.swing.JLabel networkOpsLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel opTitleLabel;
    private javax.swing.JPanel optionBodyPanel;
    private javax.swing.JPanel optionChooserPanel;
    private javax.swing.JPanel optionPageTitlePanel;
    // End of variables declaration//GEN-END:variables
}
